package com.a01_2021.thousandstimes.utils;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import lombok.Data;

import java.sql.Timestamp;

/**
 * @Author E1xP@foxmail.com
 * @Version 1.0
 * @PACKAGE_NAME com.a01_2021.thousandstimes.utils
 * @CLASS_NAME AjaxResponse
 * @Description TODO Ajax的请求相应类
 * @Date 2021/2/23 下午 1:13
 **/
@Data
public class AjaxResponse {

    private Timestamp timestamp;//时间戳

    private int status;     //响应码

    private String error;   //错误信息

    private String message;//结果描述信息

    private Object data;    //返还的数据结构

    private AjaxResponse() {
    }

    /**
     * 请求出现异常时的响应数据
     *
     * @param e 自定义异常CustomException
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse error(CustomException e) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(e.getCode());
        resultBean.setError(e.getMessage());
        return resultBean;
    }

    /**
     * 请求出现异常时的响应数据
     *
     * @param e            自定义异常CustomException
     * @param errorMessage 错误信息String
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse error(CustomException e, String errorMessage) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(e.getCode());
        resultBean.setError(errorMessage);
        return resultBean;
    }

    /**
     * 请求出现异常时的响应数据
     *
     * @param e            自定义异常类型CustomExceptionType
     * @param errorMessage errorMessage 错误信息String
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse error(CustomExceptionType e, String errorMessage) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(e.getCode());
        resultBean.setError(errorMessage);
        return resultBean;
    }

    /**
     * 请求出现异常时的响应数据
     *
     * @param errorMessage 错误信息String
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse error(String errorMessage) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(403);
        resultBean.setError(errorMessage);
        return resultBean;
    }

    /**
     * 请求出现异常时的响应数据
     *
     * @param code         状态码
     * @param errorMessage 错误信息String
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse error(int code, String errorMessage) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(code);
        resultBean.setError(errorMessage);
        return resultBean;
    }

    /**
     * 成功响应数据封装
     *
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse success() {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(200);
        resultBean.setMessage("请求响应成功！");
        return resultBean;
    }

    /**
     * 成功响应数据封装
     *
     * @param obj 数据data实体
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse success(Object obj) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(200);
        resultBean.setMessage("请求响应成功！");
        resultBean.setData(obj);
        return resultBean;
    }

    /**
     * 成功响应数据封装
     *
     * @param obj     数据data实体
     * @param message 返还消息
     * @return 返还数据封装AjaxResponse
     */
    public static AjaxResponse success(Object obj, String message) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setTimestamp(new Timestamp(System.currentTimeMillis()));
        resultBean.setStatus(200);
        resultBean.setMessage(message);
        resultBean.setData(obj);
        return resultBean;
    }
}
